﻿using System;
using System.Net;

namespace Coding4Fun.Cannon.TechEdPhone.Complete.Service
{
	public class CannonService
	{

		const string endPointServer = "betty2";
		const string endPointDirectory = "betty";

		private void CallService(ServerCommands command,
			string format = "", params object[] args)
		{
			var request = (HttpWebRequest)WebRequest.Create(
				string.Format("http://{1}/{2}/?command={0}&",
				 command, endPointServer, endPointDirectory)
				+
				string.Format(format.TrimStart('&'), args));

			request.Method = "GET";

			request.BeginGetResponse(CallBackNoResults, null);
		}
		static void CallBackNoResults(IAsyncResult result) { }

		public void Aim(double pan, double tilt)
		{
			CallService(ServerCommands.aim,
			   "pan={0}&tilt={1}", pan, tilt);
		}

		public void Drive(int leftPower, int rightPower)
		{
			CallService(ServerCommands.drive,
			   "leftPower={0}&rightPower={1}", leftPower, rightPower);
		}

		public void Fire()
		{
			CallService(ServerCommands.fire);
		}


		private enum ServerCommands
		{
			aim,
			fire,
			drive
		}
	}
}
